package com.filetransfer.view.Components;
import com.filetransfer.config.UserThemeConfiguration;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.function.BiConsumer;
@Slf4j
public class MessageInputDialog extends JDialog {
    public MessageInputDialog(JFrame parentFrame, UserThemeConfiguration.UploadHistory history,
                              BiConsumer<UserThemeConfiguration.UploadHistory, String> onConfirm) {
        super(parentFrame, "输入上传消息", true);
        setSize(400, 300);
        setLocationRelativeTo(parentFrame);
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        JLabel infoLabel = new JLabel("文件名: " + history.getFileName());
        mainPanel.add(infoLabel);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        JTextArea messageArea = new JTextArea(6, 20);
        messageArea.setLineWrap(true);
        messageArea.setWrapStyleWord(true);
        messageArea.setBorder(new EmptyBorder(5, 5, 5, 5));
        JScrollPane scrollPane = new JScrollPane(messageArea);
        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
        mainPanel.add(scrollPane);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        JButton confirmButton = new JButton("确认");
        confirmButton.addActionListener(e -> {
            String userMessage = messageArea.getText().trim();
            if (!userMessage.isEmpty()) {
                onConfirm.accept(history, userMessage);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "请填写消息内容！", "提示", JOptionPane.WARNING_MESSAGE);
            }
        });
        mainPanel.add(confirmButton);
        add(mainPanel);
    }
}
